// 快递站增删改查
const Station = require('../db/models/station');
const multiparty = require('multiparty');//文件上传模块
const path = require('path');
const fs = require("fs")
const config = require('../config/config');
module.exports = {
    GetStations(req, res) {
        Station.findAll()
            .then(data => {
                res.json({
                    code: 200,
                    message: '获取成功',
                    data: data
                });
            })
            .catch(err => {
                res.status(500).json({
                    code: 500,
                    message: '获取失败',
                    error: err
                });
            });
    },

    GetStation(req, res) {
        Station.findById(req.params.id)
            .then(data => {
                if (data == null) {
                    return res.status(404).json({
                        code: 404,
                        message: '未找到站点'
                    });
                }
                res.json({
                    code: 200,
                    message: '获取成功',
                    data: data
                });
            })
            .catch(err => {
                res.status(500).json({
                    code: 500,
                    message: '获取失败',
                    error: err
                });
            });
    },

    AddStation(req, res) {
        const station = new Station({
            name: req.body.name,
            avatar: req.body.avatar,
        });

        station.save()
            .then(data => {
                res.status(201).json({
                    code: 201,
                    message: '创建成功',
                    data: data
                });
            })
            .catch(err => {
                res.status(500).json({
                    code: 500,
                    message: '创建失败',
                    error: err
                });
            });
    },

    UpdateStation(req, res) {

        const id = req.params.id;
        const { name, avatar } = req.body;
        Station.update({ name: name, avatar }, {
            where: { id: id }
        })
            .then(data => {
                res.json({
                    code: 200,
                    message: '更新成功',
                    data: data
                });
            })
            .catch(err => {
                res.status(500).json({
                    code: 500,
                    message: '更新失败',
                    error: err
                });
            });
    },

    DeleteStation(req, res) {
        const id = req.params.id;
        Station.destroy({
            where: { id: id }
        })
            .then(data => {
                res.json({
                    code: 200,
                    message: '删除成功',
                    data: data
                });
            })
            .catch(err => {
                res.status(500).json({
                    code: 500,
                    message: '删除失败',
                    error: err
                });
            });
    },

    // 上传头像
    UploadAvatar(req, res) {
        const form = new multiparty.Form({ uploadDir: './public/avatar' });

        form.parse(req, (err, fields, files) => {
            if (err) {
                return res.status(500).json({
                    code: 500,
                    message: '文件上传失败',
                    error: err
                });
            }

            const file = files.file[0];
            if (!file) {
                return res.status(400).json({
                    code: 400,
                    message: '请选择文件'
                });
            }

            // 判断头部是文件上传
            if (file.headers['content-type'].indexOf('image') === -1) {
                // 删除非图片文件
                fs.unlinkSync(file.path);
                return res.status(400).json({
                    code: 400,
                    message: '请选择图片文件'
                });
            }

            // 返回文件信息，包括相对路径
            const relativePath = config.host.url + ':' + config.host.port + `/public/avatar/${path.basename(file.path)}`;
            res.json({
                code: 200,
                message: '上传成功',
                data: {
                    originalFilename: file.originalFilename,
                    path: relativePath
                }
            });
        });
    },

    // 删除头像
    DeleteAvatar(req, res) {
        // 将/public/avatar/xxx.jpg转换为./public/avatar/xxx.jpg
        req.body.path = `.${req.body.path}`;
        fs.unlink(req.body.path
            , (err) => {
                if (err) {
                    return res.status(500).json({
                        code: 500,
                        message: '删除失败',
                        error: err
                    });
                }
                res.json({
                    code: 200,
                    message: '删除成功'
                });
            });
    }
}